using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace Microsoft.ACEEngineering.Utility
{
    /// <summary>
    /// Contains commonly used methods.
    /// </summary>
    sealed public class CommonProcess
    {
        private  CommonProcess()
        {
        }
        /// <summary>
        /// Iterates through specified List control and returns its selected values in comma seperated format.
        /// </summary>
        /// <param name="ddlList">Listcontrol</param>
        /// <returns>Returns a comma separated string</returns>
        public static string GetCommaSeparatedValues(ListItemCollection ddlList, int startIndex)
        {
            try
            {
                StringBuilder configValues = new StringBuilder();
                int itemCount = ddlList.Count;
                for (int index = startIndex; index <= itemCount - 1; index++)
                {                    
                    configValues.Append(ddlList[index].Value);
                    configValues.Append(",");            
                }
                //remove the comma that gets appended at the end
                //configValues = configValues.Remove(configValues.Length - 1, 1);
                return configValues.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Iterates through specified List control and returns its selected values in comma seperated format.
        /// </summary>
        /// <param name="ddlList">Listcontrol</param>
        /// <returns>Returns a comma separated string</returns>
        public static string GetCommaSeparatedValuesofSelectedItems(ListBox lstCntrl)
        {
            try
            {
                StringBuilder configValues = new StringBuilder();
                int itemCount = lstCntrl.Items.Count;
                for (int index = 0; index <= itemCount - 1; index++)
                {
                    if (lstCntrl.Items[index].Selected == true)
                    {
                        configValues.Append(lstCntrl.Items[index].Value);
                        configValues.Append(",");
                    }
                }
                
                return configValues.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string GetANDSeparatedValuesofSelectedItems(ListBox lstCntrl)
        {
            try
            {
                StringBuilder configValues = new StringBuilder();
                int itemCount = lstCntrl.Items.Count;

                configValues.Append("\"");

                for (int index = 0; index <= itemCount - 1; index++)
                {
                    if (lstCntrl.Items[index].Selected == true)
                    {
                        configValues.Append(lstCntrl.Items[index].Value);
                        configValues.Append("\" AND \"");
                    }
                }

                int intCount = configValues.Length;
                if (intCount != 1)
                {
                    intCount = intCount - 6;
                    configValues.Remove(intCount, 6);
                }
                else
                {
                    configValues.Remove(0, 1);
                }

                return configValues.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string GetBinaryListofSelectedItems(ListBox lstCntrl)
        {
            try
            {
                StringBuilder configValues = new StringBuilder();
                int itemCount = lstCntrl.Items.Count;

                for (int index = 0; index <= itemCount - 1; index++)
                {
                    if (lstCntrl.Items[index].Selected == true)
                    {
                        configValues.Append('1');
                    }
                    else
                    {
                        configValues.Append('0');
                    }
                }

                return configValues.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// To display message box from server side.
        /// </summary>
        /// <param name="pge"></param>
        /// <param name="message"></param>
        /// <param name="key"></param>
        public static void CreateMessageAlert(ref System.Web.UI.Page pge,string message,string key)
        {
            string msg = message;
             msg = msg.Replace("'", "\'");
             string script = "<script language=\"JavaScript\">alert('" + msg + "')</script>";

            if (!pge.IsStartupScriptRegistered(key))
                pge.RegisterStartupScript(key, script);
    
        }
        public static void CreateConfirmationAlert(ref System.Web.UI.Page pge, string message, string key)
        {
            string msg = message;
            msg = msg.Replace("'", "\'");
            string script = "<script language=\"JavaScript\">return confirm('" + msg + "')</script>";

            if (!pge.IsStartupScriptRegistered(key))
                pge.RegisterStartupScript(key, script);

        }

        /// <summary>
        /// Loops through the DropDownList control items and comma separates them
        /// </summary>
        /// <returns>String as comma separated values</returns>
        public static string CommaSeparateValues(DropDownList ddlList)
        {
            int itemCount = 0;
            itemCount = ddlList.Items.Count;
            StringBuilder configValues = new StringBuilder();
            for (int i = 0; i <= itemCount - 1; i++)
            {
                configValues.Append(ddlList.Items[i].Text);
                configValues.Append(",");
            }
            //remove the comma that gets appended at the end
            configValues = configValues.Remove(configValues.Length - 1, 1);
            return configValues.ToString();
        }

        public static void APMErrorHandling(Exception ex, bool blLocal, Page pge, HttpSessionState httpSessionStateobject, HttpRequest httpRequestobject)
        {
            string strErrMsg, strErrorType;
            strErrorType = ex.Message.ToString().Substring(0, 8);

            if (blLocal || strErrorType == "APMError")
            {
                switch (ex.Message.ToString())
                {
                    case "APMError001":
                        strErrMsg = "Organization already exists at the selected level.";
                        break;
                    case "APMError002":
                        strErrMsg = "Top Level Organizations cannot be moved to the lower level.";
                        break;
                    case "APMError003":
                        strErrMsg = "The Application Name with the same version number already exists.";
                        break;
                    case "APMError004":
                        strErrMsg = "Server-side validation failed for application attributes.";
                        break;
                    case "APMError005":
                        strErrMsg = "Server-side validation failed.";
                        break;
                    case "APMError006":
                        strErrMsg = "Select at least one Score Range to delete.";
                        break;
                    case "APMError007":
                        strErrMsg = "One or more selected score ranges have tasks mapped to it and cannot be deleted.";
                        break;
                    case "APMError008":
                        strErrMsg = "The new defined score threshold conflicts with existing threshold value in system.";
                        break;
                    case "APMError009":
                        strErrMsg = "The Score Range Name already exists in the system.";
                        break;
                    case "APMError010":
                        strErrMsg = "The user alias already exists.";
                        break;
                    case "APMError011":
                        strErrMsg = "No application found.";
                        break;
                    case "APMError012":
                        strErrMsg = "Invalid Input: Description field exceeds maximum length of 1000 characters.";
                        break;
                    case "APMError013":
                        strErrMsg = "The application name entered is a duplicate of an application ID.  Please try another application name.";
                        break;
                    case "APMError014":
                        strErrMsg = "Duplicate Contact Type was entered.";
                        break;
                    case "APMError015":
                        strErrMsg = "The application requires at least one contact.";
                        break;
                    case "APMError016":
                        strErrMsg = "The application's Risk Assessment data not found in the system.";
                        break;
                    case "APMError017":
                        strErrMsg = "Application's Risk Assessment score has already been entered.";
                        break;
                    case "APMError018":
                        strErrMsg = "Duplicate question for the category has been entered.";
                        break;
                    case "APMError019":
                        strErrMsg = "Duplicate answer for the question has been entered.";
                        break;
                    case "APMError020":
                        strErrMsg = "Duplicate category has been entered.";
                        break;
                    case "APMError021":
                        strErrMsg = "Please correct the following:\\n- Invalid Application ID was entered.";
                        break;
                    case "APMError022":
                        strErrMsg = "Please correct the following:\\n- Invalid Input: Application Name/ID.";
                        break;
                    case "APMError023":
                        strErrMsg = "Lower Level Organizations cannot be moved to the top level.";
                        break;
                    case "APMError030":
                        strErrMsg = "Remove the highlighted Application Properties that are now disabled in system.";
                        break;
                    case "APMError031":
                        strErrMsg = "Edit the highlighted Application Contact Types that are now disabled in system.";
                        break;
                    default:
                        strErrMsg = ex.Message.ToString();
                        break;
                }
                Microsoft.ACEEngineering.Utility.CommonProcess.CreateMessageAlert(ref pge, strErrMsg, "errorkey");
            }
            else
            {
                //Log the error into event log before clearing the data.
                Microsoft.ACEEngineering.SPM.ErrorHandling.LogManager log = new Microsoft.ACEEngineering.SPM.ErrorHandling.LogManager();
                log.LogErrorData(ex, httpSessionStateobject, httpRequestobject);
                log = null;
            }

        }
    }
}
